package com.example.auv_660;

import androidx.annotation.RequiresPermission;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.ImageView;

import com.example.auv_660.service.MyApplication;
import com.example.auv_660.util.Constants;
import com.example.auv_660.util.Permission;

import java.io.IOException;

public class SplashActivity extends Activity {
    private static final int WHAT_DELAY = 0x11;     // 启动页的延时跳转
    private static final int DELAY_TIME = 5000;     // 延时时间
    private static SplashActivity instance;
    public static SplashActivity getInstance()
    {
        return instance;
    }
    private ImageView animationTV;
    private AnimationDrawable animationDrawable;
    private MediaPlayer mp0 = null;
    private MediaPlayer mp1 = null;
    private MediaPlayer mp2 = null;

    // 创建Handler对象，处理接收的消息
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case WHAT_DELAY:                    // 延时5秒跳转
                    goHome();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        MyApplication.setMainActivity(SplashActivity.this);

        // 判断安卓版本是否大于9，Android 10 （API 级别 29） 及更高版本对后台应用可启动 Activity 的时间施加限制
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
        {
            if (!Settings.canDrawOverlays(this))
            {
                // 若未授权则请求权限
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 0);
            }
        }

        mp0 = MediaPlayer.create(this, R.raw.water);
        mp1 = MediaPlayer.create(this, R.raw.set);
        mp2 = MediaPlayer.create(this, R.raw.duang);

        try
        {
            mp0.prepare();
            mp1.prepare();
            mp2.prepare();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {

        }
        mp1.setOnCompletionListener(onCompletionListener1);
        mp2.setOnCompletionListener(onCompletionListener2);

        instance = this;

        while (!Permission.checkPermission(Constants.PERMISSIONS))
        {
            Permission.requestPermission();
        }

        Constants.PHONEIMEI = getPhoneIMEI();
        Constants.FILE_PATH = getFilePath();
        System.out.println("FILE_PATH: " + Constants.FILE_PATH);

        mp1.start();
        mp0.setVolume(0.25f, 0.25f);
        mp0.start();

        animationTV = findViewById(R.id.animationTV);
        animationTV.setImageResource(R.drawable.splash);
        animationDrawable = (AnimationDrawable) animationTV.getDrawable();
        animationDrawable.start();
        // 调用handler的sendEmptyMessageDelayed方法
        handler.sendEmptyMessageDelayed(WHAT_DELAY, DELAY_TIME);
    }

    MediaPlayer.OnCompletionListener onCompletionListener1 = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp2.setVolume(1.0f, 1.0f);
            mp2.start();
        }
    };

    MediaPlayer.OnCompletionListener onCompletionListener2 = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp0.stop();
        }
    };

    @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
    private String getPhoneIMEI()
    {
        TelephonyManager telephonyManager = (TelephonyManager)
                SplashActivity.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getImei();
        if (TextUtils.isEmpty(deviceId))
        {
            deviceId = Settings.System.getString(SplashActivity.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return deviceId;
    }

    private String getFilePath()
    {
        return Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/" + SplashActivity.getInstance().getApplicationContext().getPackageName() + "/";
    }

    private void goHome()
    {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mp0.stop();
        mp1.stop();
        mp2.stop();
    }
}
